import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

/**
 * Element UI 风格的按钮组件
 * 
 * 支持：
 * - 多种按钮类型（default, primary, success, warning, danger, info）
 * - 多种尺寸（large, medium, small, mini）
 * - 加载状态、禁用状态
 * - 主题系统集成
 */
Button {
    id: elementButton
    
    // Element UI 按钮属性
    property string buttonType: "default"  // default, primary, success, warning, danger, info
    property string size: "medium"         // large, medium, small, mini
    property bool loading: false           // 加载状态
    property string icon: ""               // 图标（暂未实现）
    property bool round: false             // 圆形按钮
    property bool circle: false            // 圆形按钮（只显示图标）
    property bool plain: false             // 朴素按钮
    
    // 主题变量（通过Python主题管理器注入）
    property var themeColors: ({
        "default": {
            "background": "#FFFFFF",
            "border": "#DCDFE6", 
            "text": "#606266"
        },
        "primary": {
            "background": "#409EFF",
            "border": "#409EFF",
            "text": "#FFFFFF"
        },
        "success": {
            "background": "#67C23A",
            "border": "#67C23A",
            "text": "#FFFFFF"
        },
        "warning": {
            "background": "#E6A23C",
            "border": "#E6A23C",
            "text": "#FFFFFF"
        },
        "danger": {
            "background": "#F56C6C",
            "border": "#F56C6C",
            "text": "#FFFFFF"
        },
        "info": {
            "background": "#909399",
            "border": "#909399",
            "text": "#FFFFFF"
        }
    })
    
    property var sizeMappings: ({
        "large": { "height": 40, "padding": 12, "fontSize": 16 },
        "medium": { "height": 36, "padding": 10, "fontSize": 14 },
        "small": { "height": 32, "padding": 8, "fontSize": 13 },
        "mini": { "height": 28, "padding": 6, "fontSize": 12 }
    })
    
    // 计算当前样式
    readonly property var currentColors: themeColors[buttonType] || themeColors["default"]
    readonly property var currentSize: sizeMappings[size] || sizeMappings["medium"]
    
    // 基本属性
    height: currentSize.height
    implicitWidth: Math.max(80, contentItem.implicitWidth + leftPadding + rightPadding)
    
    leftPadding: currentSize.padding
    rightPadding: currentSize.padding
    topPadding: 0
    bottomPadding: 0
    
    // 背景样式
    background: Rectangle {
        id: backgroundRect
        
        color: {
            if (!elementButton.enabled) {
                return "#F5F7FA"  // 禁用状态
            }
            if (elementButton.pressed) {
                return Qt.darker(currentColors.background, 1.1)
            }
            if (elementButton.hovered) {
                return Qt.lighter(currentColors.background, 1.1)
            }
            return currentColors.background
        }
        
        border.width: 1
        border.color: {
            if (!elementButton.enabled) {
                return "#E4E7ED"  // 禁用状态
            }
            if (elementButton.pressed) {
                return Qt.darker(currentColors.border, 1.1)
            }
            if (elementButton.hovered) {
                return Qt.lighter(currentColors.border, 1.1)
            }
            return currentColors.border
        }
        
        radius: circle ? height / 2 : (round ? height / 2 : 4)
        
        // 加载动画
        Rectangle {
            id: loadingOverlay
            anchors.fill: parent
            color: "transparent"
            radius: parent.radius
            visible: elementButton.loading
            
            // 简单的加载动画效果
            Rectangle {
                id: loadingDot
                width: 6
                height: 6
                radius: 3
                color: currentColors.text
                anchors.centerIn: parent
                
                SequentialAnimation on opacity {
                    running: elementButton.loading
                    loops: Animation.Infinite
                    NumberAnimation { from: 0.3; to: 1.0; duration: 600 }
                    NumberAnimation { from: 1.0; to: 0.3; duration: 600 }
                }
            }
        }
        
        // 鼠标悬停动画
        Behavior on color {
            ColorAnimation { duration: 200 }
        }
        
        Behavior on border.color {
            ColorAnimation { duration: 200 }
        }
    }
    
    // 文本样式
    contentItem: Text {
        text: elementButton.text
        font.pixelSize: currentSize.fontSize
        font.family: "Microsoft YaHei"
        
        color: {
            if (!elementButton.enabled) {
                return "#C0C4CC"  // 禁用状态
            }
            return currentColors.text
        }
        
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        
        visible: !elementButton.loading
        
        Behavior on color {
            ColorAnimation { duration: 200 }
        }
    }
    
    // 禁用鼠标悬停效果（由自定义背景处理）
    hoverEnabled: true
    
    // 自定义动画
    Behavior on scale {
        NumberAnimation { duration: 100 }
    }
    
    // 按下时的缩放效果
    onPressedChanged: {
        if (pressed) {
            scale = 0.98
        } else {
            scale = 1.0
        }
    }
}
